/*
 * Copyright (C) 2016 yixun All Rights Reserved.
 * 
 * CustomerServiceImpl.java
 */
package com.emm.yixun.basic.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.basic.enums.ModelEnum;
import com.emm.yixun.basic.mapper.CustomerMapper;
import com.emm.yixun.basic.mapper.NeedReceiveRefundMapper;
import com.emm.yixun.basic.service.*;
import com.emm.yixun.common.enums.SalesReportDto;
import com.emm.yixun.common.model.*;
import com.emm.yixun.common.model.appDto.AuditListDto;
import com.emm.yixun.common.model.appDto.CustomerCountDto;
import com.emm.yixun.common.model.dto.*;
import com.emm.yixun.common.model.enums.*;
import com.emm.yixun.common.model.pmo.ClientData;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * [文件名称]<br>
 * CustomerServiceImpl.java<br>
 * <br>
 * [文件描述]<br>
 * 客户信息表 service interface impl<br>
 * <br>
 * [修改记录]<br>
 * 2016-05-11 11:27:13 创建 陶俊代码生成器<br>
 * 
 * @author 陶俊代码生成器
 * @version 1.00
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

	private final Logger logger = LoggerFactory.getLogger(CustomerServiceImpl.class);

	@Autowired
	private CustomerMapper mapper;
	@Autowired
	private ModelKeyService modelKeyService;
	@Autowired
	private PayDepositService payDepositService;
	@Autowired
	private SubscribeService subscribeService;
	@Autowired
	private SignedService signedService;

	@Autowired
	private NeedReceiveRefundMapper needReceiveRefundMapper;

	/**
	 * @param entity
	 *            搜索条件
	 * @param begin
	 *            分页开始行数[不包含]
	 * @param end
	 *            分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<Customer> getPageModel(CustomerDto entity, int begin, int end) {
		Integer total = mapper.count(entity);
		List<Customer> rows = mapper.list(entity, begin, end);
		Page<Customer> msr = new Page<Customer>(total, rows);
		msr.setPageSize(end);
		return msr;
	}

	/**
	 * 查询列表
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public List<Customer> selectListByDto(CustomerDto entity) {
		return mapper.selectListByDto(entity);
	}

	/**
	 * 查询单个
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public Customer selectByDto(CustomerDto entity) {
		return mapper.selectByDto(entity);
	}

	/**
	 * @param id
	 *            搜索条件
	 * @return 分页记录
	 */
	public Customer selectById(Long id) {
		if (id != null) {
			return mapper.selectById(id);
		}
		return null;
	}

	/**
	 * @param entity
	 *            新增记录的详细信息
	 * @return 新增行数
	 */
	public CustomerDto create(CustomerDto entity) {
		entity.setCustomerCode(modelKeyService.next(ModelEnum.CUSTOMER));
		entity.setCreateTime(Calendar.getInstance().getTime());
		entity.setUpdateTime(Calendar.getInstance().getTime());
		//记录客户新增 的编辑时间
		entity.setFillInfoTime(Calendar.getInstance().getTime());
		mapper.add(entity);
		return entity;
	}

	/**
	 * @param entity
	 *            更新记录的详细信息
	 * @return 更新行数
	 */
	public CustomerDto update(CustomerDto entity) {
		entity.setUpdateTime(Calendar.getInstance().getTime());
		mapper.edit(entity);
		return entity;
	}

	/**
	 * @param entity
	 *            待删除的记录信息
	 * @return 删除行数
	 */
	public int destroy(CustomerDto entity) {
		return mapper.del(entity);
	}

	/**
	 * @param entity
	 *            新增或更新
	 * @return 新增行数
	 */
	public int saveOrUpdate(CustomerDto entity) {
		if (entity != null && entity.getId() != null) {
			update(entity);
			return 0;
		} else {
			create(entity);
			return 1;
		}
	}

	/**
	 * @param entity
	 *            搜索条件
	 * @param begin
	 *            分页开始行数[不包含]
	 * @param end
	 *            分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<Customer> getCustomerList(CustomerDto entity, int begin, int end) {
		Integer total = mapper.getCustomerCount(entity);
		List<Customer> rows = mapper.getCustomerList(entity, begin, end);
		Page<Customer> msr = new Page<Customer>(total, rows);
		return msr;
	}

	/**
	 * 查询客户列表,根据姓名或电话进行模糊查询
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public List<Customer> getCustomerByFuzzyQuery(CustomerDto entity) {
		return mapper.getCustomerByFuzzyQuery(entity);
	}

	/**
	 * @param entity
	 *            搜索条件
	 * @param begin
	 *            分页开始行数[不包含]
	 * @param end
	 *            分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<AuditListDto> getAuditEntityList(CustomerCountDto entity, int begin, int end) {
		Integer total = mapper.selectByAuditCount(entity);
		List<AuditListDto> rows = mapper.selectByAuditList(entity, begin, end);
		Page<AuditListDto> msr = new Page<AuditListDto>(total, rows);
		return msr;
	}

	/**
	 * @param entity
	 *            搜索条件
	 * @param begin
	 *            分页开始行数[不包含]
	 * @param end
	 *            分页结束行数[包含]
	 * @return 分页记录
	 */
	@Transactional(readOnly = true)
	public Page<Customer> getCustomerCountList(CustomerCountDto entity, int begin, int end) {
		Integer total = mapper.selectCustomerByCount(entity);
		List<Customer> rows = mapper.selectCustomerByList(entity, begin, end);
		Page<Customer> msr = new Page<Customer>(total, rows);
		return msr;
	}

	public Page<CustomerComponentDto> selectCustomerComponentPage(CustomerComponentDto entity, int begin, int end) {
		Integer total = mapper.selectCustomerComponentPageCount(entity);
		List<CustomerComponentDto> rows = mapper.selectCustomerComponentPage(entity, begin, end);
		Page<CustomerComponentDto> msr = new Page<CustomerComponentDto>(total, rows);
		msr.setPageSize(end);
		return msr;
	}

	public Page<CustomerComponentDto> selectCustomerPage(CustomerComponentDto entity, int begin, int end) {
		Integer total = mapper.selectCustomerPageCount(entity);
		if(entity.isExport()){
			begin = 0;
			end = total;
		}
		List<CustomerComponentDto> rows = mapper.selectCustomerPage(entity, begin, end);
		Page<CustomerComponentDto> msr = new Page<CustomerComponentDto>(total, rows);
		msr.setPageSize(end);
		return msr;
	}
	
	public List<CustomerComponentDto> selectCustomerAll(CustomerComponentDto entity) {
		List<CustomerComponentDto> rows = mapper.selectCustomerAll(entity);
		return rows;
	}

	public List<Project> selectCustomerProject(CustomerComponentDto entity) {
		List<Project> rows = mapper.selectCustomerProject(entity);
		return rows;
	}

	/**
	 * 客户交易状态更新（customerTradeStatus） 客户交易状态:[{key:customer_trade_status,values:[{no:0,alias:BEFORE_TRADE,remark:未交易},
	 * {no:1,alias:RESERVE,remark:小订/认筹},{no:2,alias:SUBSCRIPTION,remark:认购},
	 * {no:3,alias:SIGN,remark:签约},{no:4,alias:MORE_TRADE,remark:多套交易}]}]
	 * 
	 * @param bugType
	 *            业务类型： 1 认筹、2 认购、3 签约、4退房
	 * @param busId
	 *            对应业务类型Id
	 * @param customerId
	 *            客户Id
	 * @param userId
	 *            用户Id
	 * @return Customer 客户实体对象
	 */
	public int getCustomerEntity(String bugType, Long busId, Long customerId, Long userId) {
		// 0、判断客户原有状态: 若已经是多套交易,可直接返回
		/*
		 * Customer customer = mapper.selectById(customerId); if(customer != null){
		 * if(CustomerTradeStatusEnum.MORE_TRADE.getNo().equals(customer.getCustomerTradeStatus())){ return 1; } }
		 */
		// 1、客户相关状态更新
		CustomerDto cusDto = new CustomerDto();
		cusDto.setId(customerId);
		// 1.1、根据bugType,初始化客户状态及交易状态
		if ("1".equals(bugType)) {
			cusDto.setCustomerStatus(CustomerStatusEnum.RENCHOU.getNo());
			cusDto.setCustomerTradeStatus(CustomerTradeStatusEnum.RESERVE.getNo());
		} else if ("2".equals(bugType)) {
			cusDto.setCustomerStatus(CustomerStatusEnum.RENGOU.getNo());
			cusDto.setCustomerTradeStatus(CustomerTradeStatusEnum.SUBSCRIPTION.getNo());
		} else if ("3".equals(bugType)) {
			cusDto.setCustomerStatus(CustomerStatusEnum.QIANYUE.getNo());
			cusDto.setCustomerTradeStatus(CustomerTradeStatusEnum.SIGN.getNo());
		} else if ("4".equals(bugType)) {
			cusDto.setCustomerStatus(CustomerStatusEnum.VACATE.getNo());
			cusDto.setCustomerTradeStatus(CustomerTradeStatusEnum.VACATE.getNo());
		}
		cusDto.setUpdateUserId(userId);
		cusDto.setUpdateTime(Calendar.getInstance().getTime());

		// 房源Set
		Set<Long> roomSet = new HashSet<Long>();
		// 1、查询客户签约审核成功的记录,数量超过1条,客户即为多套交易
		SignedDto signedDto = new SignedDto();
		signedDto.setAuditStatus(AuditStatusEnum.SUCCESS.getNo());
		signedDto.setCustomerId(customerId);
		List<Signed> signedList = signedService.selectListByDto(signedDto);
		if (null != signedList && signedList.size() > 1) {
			// 更新客户交易状态：多套交易
			cusDto.setCustomerTradeStatus(CustomerTradeStatusEnum.MORE_TRADE.getNo());
			return mapper.edit(cusDto);
		} else if (signedList.size() == 1) {
			// 如果只有一条签约数据,将签约房源数据存入roomSet
			roomSet.add(signedList.get(0).getRoomId());
		}

		// 2、查询客户认购成功记录,数量超过1条,客户即为多套交易
		SubscribeDto subscribeDto = new SubscribeDto();
		subscribeDto.setCustomerId(customerId);
		subscribeDto.setAuditStatus(AuditStatusEnum.SUCCESS.getNo());
		List<Subscribe> subscribeList = subscribeService.selectListByDto(subscribeDto);
		if (null != subscribeList && subscribeList.size() > 1) {
			// 更新客户交易状态：多套交易
			cusDto.setCustomerTradeStatus(CustomerTradeStatusEnum.MORE_TRADE.getNo());
			return mapper.edit(cusDto);
		} else if (subscribeList.size() == 1) {
			// 如果只有一条认购数据,将认购房源数据存入roomSet
			roomSet.add(subscribeList.get(0).getRoomId());
		}

		// 3、查询客户认筹记录,数量超过1条,客户即为多套交易
		// 3.1、由于客户不能认筹同一个房源多次,因此存在多条成功认筹记录,也为多套交易
		PayDepositDto payDepositDto = new PayDepositDto();
		payDepositDto.setCustomerId(customerId);
		payDepositDto.setAuditStatus(AuditStatusEnum.SUCCESS.getNo());
		List<PayDeposit> payDepositList = payDepositService.selectListByDto(payDepositDto);
		if (null != payDepositList && payDepositList.size() > 1) {
			// 更新客户交易状态：多套交易
			cusDto.setCustomerTradeStatus(CustomerTradeStatusEnum.MORE_TRADE.getNo());
			return mapper.edit(cusDto);
		} else if (payDepositList.size() == 1) {
			// 如果只有一条认筹数据,将认筹房源数据存入roomSet
			PayDeposit payDeposit = payDepositList.get(0);
			if (null != payDeposit) {
				if (null != payDeposit.getIsPaydeposit() && "1".equals(IsPaydepositEnum.YES.getNo())) {
					// 无房认筹,房源标志设为0
					roomSet.add(0L);
				} else {
					// 有房认筹,存储认筹号
					roomSet.add(payDeposit.getRoomId());
				}
			}
		}

		// 4、判断认筹、认购、签约记录,都不为多套交易
		// 4.1、判断认筹、认购、签约记录中的房源Set数量,超过1,即为多套交易
		if (roomSet.size() > 1) {
			cusDto.setCustomerTradeStatus(CustomerTradeStatusEnum.MORE_TRADE.getNo());
		}
		mapper.edit(cusDto);
		return 1;
	}

	public Page<Customer> getSubscribeCustomerList(CustomerCountDto entity, int begin, int end) {
		Integer total = mapper.selectSubscribeCustomerByCount(entity);
		List<Customer> rows = mapper.selectSubscribeCustomerByList(entity, begin, end);
		Page<Customer> msr = new Page<Customer>(total, rows);
		return msr;
	}

	public Page<Customer> getPaymentCustomerList(CustomerCountDto entity, int begin, int end) {
		Integer total = mapper.selectPaymentCustomerByCount(entity);
		List<Customer> rows = mapper.selectPaymentCustomerByList(entity, begin, end);
		Page<Customer> msr = new Page<Customer>(total, rows);
		return msr;
	}

	public Page<Customer> getImperfectCustomerList(CustomerCountDto entity, int begin, int end){
		Integer total = mapper.selectImperfectCustomerByCount(entity);
		List<Customer> rows = mapper.selectImperfectCustomerByList(entity,begin,end);
		Page<Customer> msr = new Page<Customer>(total,rows);
		return msr;
	}

	public String createTabTypeSql(String tabType, Long merchantId) {
		String appendSql = " and 1=1";
		if (StringUtils.isBlank(tabType))
			return appendSql;
		// 1 正常客户 2逾期未签约 3待处理客户 4逾期未付款
		if (tabType.equals("1")) {
			// 过滤未处理的客户
			// appendSql=" AND t.process_status<>'0' ";
		} else if (tabType.equals("2")) {
			appendSql = createTabTypeSqlBySubscribe(merchantId);
		} else if (tabType.equals("3")) {
			// 过滤已分配未处理的客户
			appendSql = " AND t.is_distribution='1' AND t.process_status='0' ";
		} else if (tabType.equals("4")) {
			appendSql = createTabTypeSqlByPayment(merchantId);
		} else if(tabType.equals("5")) {
			//填写信息不完整的客户
			appendSql =" AND t.is_intention_intact='0'";
		}
		return appendSql;
	}

	private String createTabTypeSqlBySubscribe(Long merchantId) {
		/**
		 * 1.查找业务标志为认购、认购审核通过、当前日期大于认购转签约日期的认购记录 2.拼接过滤sql
		 **/
		StringBuffer appendSql = new StringBuffer();
		SubscribeDto query = new SubscribeDto();
		query.setMerchantId(merchantId);
		query.setAuditStatus(AuditStatusEnum.SUCCESS.getNo());
		query.setBusinessStatus("1");
		query.setConversionSignDateAfter(DateUtils.addDay(new Date(), -1));
		List<Subscribe> subscribeList = subscribeService.createTabTypeSqlBySubscribe(query);
		if (null == subscribeList || subscribeList.size() == 0) {
			appendSql.append(" and 1=2 ");
			return appendSql.toString();
		}
		Map<Long, Long> customerIdMap = new HashMap<Long, Long>();
		for (Subscribe subscribe : subscribeList) {
			Long customerId = subscribe.getCustomerId();
			if (null == customerId)
				continue;
			customerIdMap.put(customerId, customerId);
		}
		appendSql.append(" and ( ");
		int index = 0;
		for (Long customerId : customerIdMap.keySet()) {
			if (index == 0) {
				appendSql.append(" t.id= " + customerId);
			} else {
				appendSql.append(" or t.id= " + customerId);
			}
			index++;
		}
		appendSql.append(" ) ");
		return appendSql.toString();
	}

	private String createTabTypeSqlByPayment(Long merchantId) {
		/**
		 * 1.查找业务标志签约、当前日期大于应收日期、应收金额大于已收金额 的应收款记录 2.拼接过滤sql
		 **/
		StringBuffer appendSql = new StringBuffer();
		NeedReceiveRefundDto needReceiveRefundDto = new NeedReceiveRefundDto();
		needReceiveRefundDto.setMerchantId(merchantId);
		needReceiveRefundDto.setBusinessType("2");
		needReceiveRefundDto.setDocType("0");
		needReceiveRefundDto.setStatus("1");
		needReceiveRefundDto.setReceivablesDateAfter(DateUtils.addDay(new Date(), -1));
		List<NeedReceiveRefund> list = needReceiveRefundMapper.selectDelayListByDto(needReceiveRefundDto);
		if (null == list || list.size() == 0) {
			appendSql.append(" and 1=2 ");
			return appendSql.toString();
		}
		Map<Long, Long> customerIdMap = new HashMap<Long, Long>();
		for (NeedReceiveRefund row : list) {
			Long customerId = row.getCustomerId();
			if (null == customerId)
				continue;
			customerIdMap.put(customerId, customerId);
		}
		appendSql.append(" and ( ");
		int index = 0;
		for (Long customerId : customerIdMap.keySet()) {
			if (index == 0) {
				appendSql.append(" t.id= " + customerId);
			} else {
				appendSql.append(" or t.id= " + customerId);
			}
			index++;
		}
		appendSql.append(" ) ");
		return appendSql.toString();
	}

	/**
	 * 查询列表
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public List<Customer> selectCustomerPhoneByListPC(CustomerDto entity) {
		return mapper.selectCustomerPhoneByListPC(entity);
	}

	/**
	 * 根据客户姓名或手机号查询客户
	 */
	public Page<CustomerComponentDto> findCustomerPageByNameOrPhone(CustomerComponentDto entity, int begin, int end) {
		Integer total = mapper.findCustomerPageByNameOrPhoneCount(entity);
		List<CustomerComponentDto> rows = mapper.findCustomerPageByNameOrPhone(entity, begin, end);
		Page<CustomerComponentDto> msr = new Page<CustomerComponentDto>(total, rows);
		msr.setPageSize(end);
		return msr;
	}

	public Integer cusCount(CustomerComponentDto entity) {
		return mapper.selectCustomerPageCount(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.emm.yixun.basic.service.CustomerService#updateBySaleGroupUser(com.emm.yixun.common.model.dto.CustomerDto)
	 */
	public int updateBySaleGroupUser(CustomerDto entity) {
		return mapper.updateBySaleGroupUser(entity);
	}

	/**
	 * 跟进客户ID查询客户业务状态
	 * 
	 * @param id
	 * @return 业务状态
	 */
	public String queryCustomerBusiness(Long id) {
		return mapper.queryCustomerBusiness(id);
	}
	
	/**
	 * 查询列表
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public List<CustomerDto> setBirthByDto(CustomerDto entity) {
		return mapper.setBirthByDto(entity);
	}

	public int findIntentionIntactCount(CustomerComponentDto dto) {
		return mapper.selectCustomerPageIntentionCount(dto);
	}

	public Page<CustomerComponentDto> selectCustomerItentionPage(CustomerComponentDto entity, int begin, int end) {
		Integer total = mapper.selectCustomerPageIntentionCount(entity);
		List<CustomerComponentDto> rows = mapper.selectCustomerIntentionPage(entity, begin, end);
		Page<CustomerComponentDto> msr = new Page<CustomerComponentDto>(total, rows);
		msr.setPageSize(end);
		return msr;
	}

	public List<ContactWayDto> queryProjectFtContactWay(ContactWayDto contactWayDto) {
	    
	    return mapper.queryProjectFtContactWay(contactWayDto);
	}

	/**
	 * 查询单个
	 * 
	 * @param entity
	 *            搜索条件
	 * @return 分页记录
	 */
	public Customer getSubjectRatio(CustomerDto entity) {
		return mapper.getSubjectRatio(entity);
	}
	
	public HashMap<String, Long> queryCustomerLevel(ContactWayDto contactWayDto) {
	    return mapper.queryCustomerLevel(contactWayDto);
	}

	public List<SalesReportDto> queryProjectSignedInfo(ContactWayDto contactWayDto) {
	    return mapper.queryProjectSignedInfo(contactWayDto);
	}

	public List<SalesReportDto> queryProjectSignedInfoByIds(ContactWayDto contactWayDto) {
		return mapper.queryProjectSignedInfoByIds(contactWayDto);
	}

	public List<ClientData> queryCustomerLevelByPmo(ClientDataParams clientDataParams) {
		return mapper.queryCustomerLevelByPmo(clientDataParams);
	}

	/**
	 * 变更客户等级
	 * @param customerId
	 * @return
	 */
	public boolean changeCustomerLevel(Long customerId, String level) {
		boolean result = true;
		boolean hasTrade = checkCustomerLevel(customerId);
		boolean isPerfect = checkCustomerPerfect(customerId);
		Customer customer = new Customer();
		customer.setId(customerId);
		if(hasTrade){
			customer.setIntentionLevel(IntentionLevelEnum.A.getNo());
		}else{
			//场景: 退认购、退签约之后无交易,将客户置位C级
			if(null != level){
				customer.setIntentionLevel(level);
			}
		}
		if(isPerfect){
			customer.setIsIntentionIntact(IntentionIntactEnum.PERFECT.getNo());
		}
		// A级并且非信息完善用户
		if(IntentionLevelEnum.A.getNo().equals(customer.getIntentionLevel()) && !isPerfect){
			customer.setIsIntentionIntact(IntentionIntactEnum.UN_PERFECT.getNo());
		}

		if(!StringUtils.isBlank(customer.getIntentionLevel())
				|| !StringUtils.isBlank(customer.getIsIntentionIntact())){
			Customer editCustomer = mapper.selectById(customerId);
			if(null != editCustomer){
				boolean needUpdate = false;
				if(StringUtils.isBlank(editCustomer.getIntentionLevel()) || StringUtils.isBlank(editCustomer.getIsIntentionIntact())){
					needUpdate = true;
				}else if(StringUtils.isBlank(customer.getIntentionLevel()) || StringUtils.isBlank(customer.getIsIntentionIntact())){
					needUpdate = true;
				}else if(!customer.getIntentionLevel().equals(editCustomer.getIntentionLevel())
						|| !customer.getIsIntentionIntact().equals(editCustomer.getIsIntentionIntact())){
					needUpdate = true;
				}
				if(needUpdate){
					logger.info("客户信息变更,客户编号: " + customerId
							+ "、原等级及完善情况: " + editCustomer.getIntentionLevel() + "," + editCustomer.getIsIntentionIntact()
							+ "、变更后等级及完善情况: " + customer.getIntentionLevel() + "," + customer.getIsIntentionIntact()
					);
					customer.setUpdateTime(Calendar.getInstance().getTime());
					int count = mapper.edit(customer);
					if(count > 0){
						result = true;
					}else{
						result = false;
					}
				}
			}

		}
		return result;
	}

	/**
	 * 校验客户是否存在有效的认购或签约交易
	 * 检验客户信息是否完整,达到A级要求
	 * @param customerId
	 * @return
	 */
	public boolean checkCustomerLevel(Long customerId) {
		boolean hasTrade = false;

		PayDepositDto payDepositDto = new PayDepositDto();
		payDepositDto.setCustomerId(customerId);
		payDepositDto.setAuditStatus(AuditStatusEnum.SUCCESS.getNo());
		payDepositDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
		List<PayDeposit> payDepositList = payDepositService.selectListByDto(payDepositDto);
		if(null != payDepositList && payDepositList.size() > 0){
			for(PayDeposit payDeposit: payDepositList){
				//存在非退数据,即说明有交易存在
				if(!PayDepositBusinessStatusEnum.REFUNDPAYDEPOSIT.getNo().equals(payDeposit.getBusinessStatus())){
					hasTrade = true;
					break;
				}
			}
		}
		if(hasTrade){
			SubscribeDto subDto = new SubscribeDto();
			subDto.setCustomerId(customerId);
			subDto.setAuditStatus(AuditStatusEnum.SUCCESS.getNo());
			subDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			List<Subscribe> subList = subscribeService.selectListByDto(subDto);
			if(null != subList && subList.size() > 0){
				for(Subscribe sub: subList){
					//存在非退数据,即说明有交易存在
					if(!SubscribeBusinessStatusEnum.REFUNDSUBSCRIPTION.getNo().equals(sub.getBusinessStatus())
							&& !SubscribeBusinessStatusEnum.REFUNDSUBSCRIPTION.getNo().equals(sub.getBusinessSecStatus())){
						hasTrade = true;
						break;
					}
				}
			}
		}

		if(!hasTrade){
			SignedDto signedDto = new SignedDto();
			signedDto.setCustomerId(customerId);
			signedDto.setAuditStatus(AuditStatusEnum.SUCCESS.getNo());
			signedDto.setDelFlag(DelFlagEnum.UNDEL.getNo());
			List<Signed> signedList = signedService.selectListByDto(signedDto);
			if(null != signedList && signedList.size() > 0){
				for(Signed signed: signedList){
					//存在非退数据,即说明有交易存在
					if(!SignedBusinessStatusEnum.CHECKWARDS.getNo().equals(signed.getBusinessStatus())
							&& !SignedBusinessStatusEnum.CHECKWARDS.getNo().equals(signed.getBusinessSecStatus())){
						hasTrade = true;
						break;
					}
				}
			}
		}

		return hasTrade;
	}

	/**
	 * 校验客户信息是否完善
	 * @param customerId
	 * @return
	 */
	public boolean checkCustomerPerfect(Long customerId) {
		boolean isPerfect = true;
		Customer customer = mapper.selectById(customerId);
		//项目名称、意向登记、客户姓名、获知途径、性别、联系电话
		//projectId、customerStar、customerName、informedWay、customerSex、customerPhone
		//接触方式、接触时间、沟通内容： 此處必須有一套有數據
		//ftContactWay、ftContactTimes、ftContactContent
		//ftContactWay1、ftContactTimes1、ftContactContent1
		//置业动机、关注重点、需求面积、生活服务、付款方式
		//rqTheirMotives、rqFocusId、rqDemandArea、rqServicesId、rqPaymentMethod
		//居住地--省市区
		//peResidenceProvinceId、peResidenceCityId、peResidenceAreaId
		//工作区--省市区
		//peWorkaroundProvinceId、peWorkaroundCityId、peWorkaroundAreaId
		//从事行业
		//congshihangye
		if(null == customer.getProjectId()
			|| StringUtils.isBlank(customer.getCustomerName())
			|| StringUtils.isBlank(customer.getInformedWay())
			|| StringUtils.isBlank(customer.getCustomerSex())
			|| StringUtils.isBlank(customer.getCustomerPhone())
			|| StringUtils.isBlank(customer.getRqTheirMotives())
			|| StringUtils.isBlank(customer.getRqFocusId())
			|| StringUtils.isBlank(customer.getRqDemandArea())
			|| StringUtils.isBlank(customer.getRqServicesId())
			|| StringUtils.isBlank(customer.getRqPaymentMethod())
			|| null == customer.getPeResidenceProvinceId()
			|| null == customer.getPeResidenceCityId()
			|| null == customer.getPeResidenceAreaId()
			|| null == customer.getPeWorkaroundProvinceId()
			|| null == customer.getPeWorkaroundCityId()
			|| null == customer.getPeWorkaroundAreaId()
			|| StringUtils.isBlank(customer.getCongshihangye())
			|| (StringUtils.isBlank(customer.getFtContactWay()) && StringUtils.isBlank(customer.getFtContactWay1()))
			|| (null == customer.getFtContactTime() && null == customer.getFtContactTime1())
			|| (StringUtils.isBlank(customer.getFtContactContent()) && StringUtils.isBlank(customer.getFtContactContent1()))
			){
			isPerfect = false;
		}

		return isPerfect;
	}

}
